/* Copyright 2017, 2018, 2019, 2020 Gabriel Czernikier
 *
 * This file is part of Côtehaus.
 * Côtehaus is free software: you can redistribute it and/or modify
 * it under the terms of one of the following:
 * 1. The same dual license as that of Côtehaus itself, of which individual licenses are
 * the MIT License and the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 2. The MIT License.
 * 3. The GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Côtehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MIT License and the GNU General Public License for more details.
 * You should have received a copy of the MIT License and the GNU General Public License
 * along with Côtehaus.  If not, see <https://opensource.org/licenses/MIT> and <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "composer.h"
#include "lgwseq.h"
#include "types.h"

int up_bounds_ary[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int alignment_ary[] = {1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1};
int majors_size_ary[NH_UP_BOUNDS_SZ];   // late-time (more recent) naming style coding changed from "majors" to something else standing for "equivalence class representative".
unsigned char *buffer[NH_UP_BOUNDS_SZ];
size_t buf_len[NH_UP_BOUNDS_SZ];
size_t buf_offset[NH_UP_BOUNDS_SZ];

void bounds_move(int idx, unsigned int amount) {
	for(;idx<NH_UP_BOUNDS_SZ; idx++)
		up_bounds_ary[idx]+=amount;
}

void
align_bounds() {
	// Adjusts bounds
	for(int i=1; i!=NH_UP_BOUNDS_SZ; i++) {
		bounds_move(i-1, /*padding*/(alignment_ary[i]-up_bounds_ary[i-1]%alignment_ary[i])%alignment_ary[i]);
	}
	// Adjustment in accordance with a clause in the header_item specification. Sic.: "data_size Size of data section in bytes. Must be an even multiple of sizeof(uint)."
	bounds_move(NH_DATA_UBOUND, (sizeof(uint32_t)*2 - (up_bounds_ary[NH_DATA_UBOUND]-up_bounds_ary[NH_DATA_LBOUND])%(sizeof(uint32_t)*2))%(sizeof(uint32_t)*2));
	// Fills the buffers up to their adjusted section's boundaries
	static const char FILLPATTERN[] = "\0\0\0\0\0\0"; // Also count on an implicit \0 automatically stored as of C language's approach to string representation. The pattern counts as many \0 as the longest sightsaw gap (pad) to fill, namely that one from the header_item's clause refered to above (i.e. a double sizeof(uint32_t)-1==7 bytes).
	for(int i=1; i!=NH_UP_BOUNDS_SZ; i++) {
		int buf_len_wout_pad= buf_offset[i];  // avoid side effects of using buf_offset in a macro that query and set its value at the same time.
		BUFFER_WRITE(buffer[i],buf_len[i],FILLPATTERN,buf_offset[i],up_bounds_ary[i]-(up_bounds_ary[i-1]+buf_len_wout_pad))
	}
}